const { resolve } = require('../../helper')
const merge = require('webpack-merge')
const BaseModules = require('../BaseModules')
const CommonModules = require('../CommonModules')
const { NormalLoaders } = require('../LoaderModules')
const fixtAssetsPath = require('../../plugins/fixtAssetsPath')
const CreateHwp = require('./renderHwp')

const { dev } = BaseModules.command
const { output, src } = BaseModules.appConf
const { plugins = [], ...baseOther } = BaseModules.userBaseWebackConf


module.exports = function(moduleGroup){

	const conf = merge({}, baseOther, {
		entry: {},
		externals: CommonModules.externals,
		module:{ noParse: [/\.min\.js$/], rules: [...NormalLoaders()] },
		output: {
			path: dev ? resolve(output) : resolve(output, moduleGroup[0].bundleName)
		},
		resolve: {
			alias: {
				'@m': resolve(src, 'modules', moduleGroup[0].bundleName)
			}
		},
		plugins: [
			...plugins,
			new fixtAssetsPath()
		]
	})

	moduleGroup.reduce((pre, cur) => {
		const { moduleName, pageName, bundleName, request, param } = cur
		const chunkName = !dev ? `${moduleName}/${pageName}` : `${bundleName}/${moduleName}/${pageName}`
		conf.entry[chunkName] = request
		conf.plugins.unshift(CreateHwp.call(BaseModules, param.title, chunkName, CommonModules.injectAssets, BaseModules.transformOutputPath()))
	}, [])

	return conf
}